package com.huabo.fxgl.service.impl;

import com.huabo.fxgl.entity.*;
import com.huabo.fxgl.mapper.RiskLevelmappingMapper;
import com.huabo.fxgl.service.IRiskAssplanRiskService;
import com.huabo.fxgl.service.IRiskLevelmappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiYe
 * @since 2022-08-01
 */
@Service
@Slf4j
public class RiskLevelmappingServiceImpl extends ServiceImpl<RiskLevelmappingMapper, RiskLevelmapping> implements IRiskLevelmappingService {
    @Autowired
    private RiskLevelmappingMapper riskLevelmappingMapper;
    @Autowired
    private IRiskAssplanRiskService riskAssplanRiskService;

    @Override
    public RiskLevelmapping getByInfluId(String id) {
        List<RiskLevelmapping> levelMappings = riskLevelmappingMapper.getByInfluId(id);
        if (null != levelMappings && levelMappings.size() > 0) {
            return levelMappings.get(0);
        }
        return null;
    }

    /**
     * 默认显示页面的详情信息
     * @author wanghongtuo
     * @Date 2022/8/10
     * @param riskcatid
     * @param planId
     * @version 1.0.1
     * @return
     */
    public Map forCycle(String riskcatid, BigDecimal planId) {
        Map result = new HashMap();
        Map<String, List<Risk>> map = new HashMap<>();
        List<RiskAssplanRisk> assPlanRisks = riskAssplanRiskService.findRiskByRiskType(riskcatid, planId);
        if (assPlanRisks.size() > 0) {
            for (RiskAssplanRisk riskAssPlanRisk : assPlanRisks) {
                String res = riskAssPlanRisk.getSeverity() + "" + riskAssPlanRisk.getFrequency();
                log.info("------------------------res: " + res);
                if (null != map.get(res)) {
                    List<Risk> risks = map.get(res);
                    risks.add(riskAssPlanRisk.getRisk());
                    map.put(res, risks);
                } else {
                    List<Risk> risks = new ArrayList<>();
                    risks.add(riskAssPlanRisk.getRisk());
                    map.put(res, risks);
                }
            }
            log.info("-------------------------------------------assPlanRisks: " + assPlanRisks);
            // 获取评估标准
            RiskAssessmentstd assessMentsTd = assPlanRisks.get(0).getAssplan().getAssstd();
            List<RiskPossibility> possibilities = new ArrayList<>();
            possibilities.addAll(assessMentsTd.getPossibilities());
            Collections.sort(possibilities, new Comparator<RiskPossibility>() {
                @Override
                public int compare(RiskPossibility o1, RiskPossibility o2) {
                    return o1.getRplevel().compareTo(o2.getRplevel());
                }
            });
            List<RiskInfludegree> infludegrees = new ArrayList<>();
            infludegrees.addAll(assessMentsTd.getRiskInfludegrees());
            Collections.sort(infludegrees, new Comparator<RiskInfludegree>() {
                @Override
                public int compare(RiskInfludegree o1, RiskInfludegree o2) {
                    return o1.getRilevel().compareTo(o2.getRilevel());
                }
            });
            Collections.reverse(infludegrees);
            // 影响程度
            for (RiskInfludegree riskInfludegree : infludegrees) {
                riskInfludegree.setRiskLevelMapping(
                        getByInfluId(riskInfludegree.getDegreeid().toString()));
            }
            for (int i = 1; i <= 5; i++) {
                if (i == 1) {
                    for (RiskInfludegree riskInfludegree : infludegrees) {
                        List<Risk> risks = map.get(riskInfludegree.getRilevel() + "" + i);
                        if (null != risks) {
                            riskInfludegree.getRiskLevelMapping().setCount1(risks.size());
                            StringBuffer sb = new StringBuffer();
                            for (Risk tblRisk : risks) {
                                sb.append(tblRisk.getRiskid() + ",");
                            }
                            riskInfludegree.getRiskLevelMapping().setRisk1Ids(sb.toString());
                        }
                    }
                }
                if (i == 2) {
                    for (RiskInfludegree riskInfludegree : infludegrees) {
                        List<Risk> risks = map.get(riskInfludegree.getRilevel() + "" + i);
                        if (null != risks) {
                            riskInfludegree.getRiskLevelMapping().setCount2(risks.size());
                            StringBuffer sb = new StringBuffer();
                            for (Risk tblRisk : risks) {
                                sb.append(tblRisk.getRiskid() + ",");
                            }
                            riskInfludegree.getRiskLevelMapping().setRisk2Ids(sb.toString());
                        }
                    }
                }
                if (i == 3) {
                    for (RiskInfludegree riskInfludegree : infludegrees) {
                        List<Risk> risks = map.get(riskInfludegree.getRilevel() + "" + i);
                        if (null != risks) {
                            riskInfludegree.getRiskLevelMapping().setCount3(risks.size());
                            StringBuffer sb = new StringBuffer();
                            for (Risk tblRisk : risks) {
                                sb.append(tblRisk.getRiskid() + ",");
                            }
                            riskInfludegree.getRiskLevelMapping().setRisk3Ids(sb.toString());
                        }
                    }
                }
                if (i == 4) {
                    for (RiskInfludegree riskInfludegree : infludegrees) {
                        List<Risk> risks = map.get(riskInfludegree.getRilevel() + "" + i);
                        if (null != risks) {
                            riskInfludegree.getRiskLevelMapping().setCount4(risks.size());
                            StringBuffer sb = new StringBuffer();
                            for (Risk tblRisk : risks) {
                                sb.append(tblRisk.getRiskid() + ",");
                            }
                            riskInfludegree.getRiskLevelMapping().setRisk4Ids(sb.toString());
                        }
                    }
                }
                if (i == 5) {
                    for (RiskInfludegree riskInfludegree : infludegrees) {
                        List<Risk> risks = map.get(riskInfludegree.getRilevel() + "" + i);
                        if (null != risks) {
                            riskInfludegree.getRiskLevelMapping().setCount5(risks.size());
                            StringBuffer sb = new StringBuffer();
                            for (Risk tblRisk : risks) {
                                sb.append(tblRisk.getRiskid() + ",");
                            }
                            riskInfludegree.getRiskLevelMapping().setRisk5Ids(sb.toString());
                        }
                    }
                }
            }
            result.put("map", map);
            if (infludegrees != null) {
                result.put("retuList", infludegrees);
            }
            result.put("infludegrees", infludegrees);
            result.put("planId", planId);
        }
        return result;
    }
}
